<?php

include_once( 'config.php' );
include_once( 'weibo/saetv2.ex.class.php' );
include_once( 'weibo/Weibo4Php.php' );

session_start();

$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

if (isset($_REQUEST['code'])) {
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	try {
		$token = $o->getAccessToken( 'code', $keys ) ;
	} catch (OAuthException $e) {
	}
}

if ($token) {
	$_SESSION['token'] = $token;
	setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
	
	$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
	$uid_get = $c->get_uid();
	$uid = $uid_get['uid'];
	$user = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
    if(isset($user['id']) && $user['id']!=0){
           $_SESSION['user'] = $user; 
//         $_SESSION['c'] = $c;
//         $trendline = new Trendline($c, $user);
//         $_SESSION['trendline'] = $trendline; 
		  $weibo4php = new Weibo4Php($c, $user);
		  $_SESSION['weibo4php'] = $weibo4php; 
         echo '<script type="text/javascript">alert("ok");location.href="'.$_SESSION['nexturl'].'";</script>';
    } else {
		 echo "授权失败。";
         echo var_dump($user);
    }	       
   	
?>


<?php
} else {
?>

授权失败。
<?php
}
?>
